package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;

import com.google.common.collect.Lists;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISigninService;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ISignupService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.SigninMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.SigninCreateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.Signin;
import org.zfes.snowier.cloud.zbss.metting.biz.model.Signup;

import java.util.stream.Stream;
@Service
public class SigninServiceImpl implements ISigninService{
 	@Autowired
	private SigninMapper signinMapper;
 	@Autowired
	private ISignupService signupService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createSignin(SigninCreateDto signinCreateDto) {
		ZBeanUtil.validateBean(signinCreateDto);
		
		Optional<Signup> signupOp =signupService.loadSignupById(signinCreateDto.getSinupId());
		ZAssert.notNull(signupOp, "未查询到报名信息");
		
		Signin signin=new Signin();
		ZBeanUtil.copy(signinCreateDto, signin, true);
		//冗余报名信息
		ZBeanUtil.copy(signupOp.get(), signin, true);
		
		ZBeanUtil.setCreateInfo(signin);
		ZBeanUtil.validateBean(signin);
		signin.setSiginTime(ZDateUtil.newDate());
		
		signinMapper.insertSelective(signin);
	}
	//状态_D#1签到成功，-1移除 #
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Signin> loadSigninById(Long id) {
		if(id!=null){
			return Optional.ofNullable(signinMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public  List<Signin> loadSigninByMetId(Long metId, Byte status){
		 if(metId==null) {
			 return Lists.newArrayList();
		 }
		return  signinMapper.selectByMetId(metId,status);
	 }
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setSigninStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		signinMapper.updateStatus( id,  status) ;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadSigninDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(signinMapper.selectListCount(pm), signinMapper.selectMapListPage(pm));
		return ds;
	}
	
}